{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 16
   },
   "source": [
    "## Numpy实用函数总结TOP50\n",
    "- 创建数组\n",
    "- 操纵数组\n",
    "- 数值替换\n",
    "- 集合操作\n",
    "- 分割\n",
    "- 合并\n",
    "- 比较\n",
    "- 重复\n",
    "- 代数计算\n",
    "- 统计分析\n",
    "- 输出设置\n",
    "- 存储导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "cell_id": 14
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 18
   },
   "source": [
    "### 创建数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 29
   },
   "source": [
    "#### 1. array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cell_id": 22
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cell_id": 21
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5], dtype=object)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4,5],dtype = 'O')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "cell_id": 20
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4,5.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "cell_id": 23
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[1, 2], [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "cell_id": 26
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1, 2, 3], ndmin=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 27
   },
   "source": [
    "#### 2. linspace\n",
    "\n",
    "\n",
    "```\n",
    "-> start  : [optional] start of interval range. By default start = 0\n",
    "-> stop   : end of interval range\n",
    "-> restep : If True, return (samples, step). By default restep = False\n",
    "-> num    : [int, optional] No. of samples to generate\n",
    "-> dtype  : type of output array\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "cell_id": 34
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(10,100,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "cell_id": 33
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.0, 3.020408163265306, 5.040816326530612, 7.061224489795918,\n",
       "       9.081632653061224, 11.102040816326529, 13.122448979591836,\n",
       "       15.142857142857142, 17.163265306122447, 19.183673469387752,\n",
       "       21.204081632653057, 23.224489795918366, 25.24489795918367,\n",
       "       27.265306122448976, 29.285714285714285, 31.30612244897959,\n",
       "       33.326530612244895, 35.3469387755102, 37.367346938775505,\n",
       "       39.38775510204081, 41.408163265306115, 43.42857142857142,\n",
       "       45.44897959183673, 47.469387755102034, 49.48979591836734,\n",
       "       51.51020408163265, 53.53061224489795, 55.55102040816326,\n",
       "       57.57142857142857, 59.59183673469387, 61.61224489795918,\n",
       "       63.63265306122448, 65.65306122448979, 67.67346938775509,\n",
       "       69.6938775510204, 71.71428571428571, 73.73469387755101,\n",
       "       75.75510204081633, 77.77551020408163, 79.79591836734693,\n",
       "       81.81632653061223, 83.83673469387755, 85.85714285714285,\n",
       "       87.87755102040815, 89.89795918367346, 91.91836734693877,\n",
       "       93.93877551020407, 95.95918367346938, 97.97959183673468, 100.0],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(start = 1,\n",
    "           stop = 100,\n",
    "           num = 50,\n",
    "           endpoint = True,\n",
    "           retstep = False,\n",
    "           dtype = 'O')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 32
   },
   "source": [
    "#### 3. arange\n",
    "根据start与stop指定的范围以及step设定的步长，生成一个 ndarray。\n",
    "\n",
    "**arange允许步长为小数，而range不允许。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cell_id": 31
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(5,10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "cell_id": 37
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0, 5, 0.5, dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "cell_id": 40
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0, 5, 0.5, dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "cell_id": 39
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(-3, 3, 0.5, dtype=int)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 38
   },
   "source": [
    "The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers. This may result in incorrect results for large integer values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "cell_id": 42
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 7776, 8801, 6176, 625, 6576, 4001]\n",
      "[0, 1, 7776, 7185, 0, 5969, 4816, 3361]\n"
     ]
    }
   ],
   "source": [
    "power = 40\n",
    "modulo = 10000\n",
    "x1 = [(n ** power) % modulo for n in range(8)]\n",
    "x2 = [(n ** power) % modulo for n in np.arange(8)]\n",
    "print(x1)  # correct\n",
    "print(x2)   # incorrect"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 45
   },
   "source": [
    "#### 4. Uniform \n",
    "\n",
    "从下限值和上限值之间的均匀分布生成随机样本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "cell_id": 13
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.58154762, 8.59377902, 8.81774804, 5.91981966])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(5,10,size = 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "cell_id": 12
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.24120233, 0.11623619, 0.69488693, 0.46414082, 0.20802988])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(size = 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "cell_id": 11
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.10856637, 0.85816618, 0.17700248],\n",
       "       [0.55111254, 0.39513718, 0.27022124]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(size = (2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 48
   },
   "source": [
    "#### 5. Random.randint\n",
    "在一个范围内生成 n 个随机整数样本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "cell_id": 15
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 8, 8, 8, 8, 5, 9, 9, 8, 7])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(5,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "cell_id": 51
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1, 1, 1, 0, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(2, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "cell_id": 50,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 4],\n",
       "       [0, 4, 2, 3]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(5, size=(2, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 53
   },
   "source": [
    "#### 6. Random.rand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "cell_id": 54
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.2489064 , 0.32299079],\n",
       "        [0.91530608, 0.20221999],\n",
       "        [0.49186829, 0.08434807]],\n",
       "\n",
       "       [[0.5168674 , 0.72400696],\n",
       "        [0.05474721, 0.5733918 ],\n",
       "        [0.20622626, 0.48802429]],\n",
       "\n",
       "       [[0.88251588, 0.6426685 ],\n",
       "        [0.92010134, 0.20009791],\n",
       "        [0.64575778, 0.34680349]],\n",
       "\n",
       "       [[0.52543559, 0.18258499],\n",
       "        [0.17784223, 0.6164311 ],\n",
       "        [0.75691732, 0.01457699]]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成一个shape为[4,3,2]的array，array中每个元素都是一个[0,1)之间的随机数\n",
    "np.random.rand(4,3,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 61
   },
   "source": [
    "#### 7. Random.randn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "cell_id": 55
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9186786353449959"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn() # 没有参数，只返回单个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "cell_id": 56
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.31181497, -1.14799424, -0.1994465 , -0.71845975],\n",
       "       [ 0.6041545 , -0.58863524, -0.54574407, -0.48875584]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(2,4) # 返回一个shape为[2,4]的array，array中的元素服从标准正态分布"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 57
   },
   "source": [
    "#### 8. Random.random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "cell_id": 58
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.07239126, 0.18838359],\n",
       "       [0.56730629, 0.49660698]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random(size = (2,2)) # 生成随机浮点数阵列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 59
   },
   "source": [
    "#### 9. Random.choice\n",
    "\n",
    "从给定数组a中随机选择，p可以指定a中每个元素被选择的概率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "cell_id": 66
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 4])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.choice(5,3) # 当a为整数时，随机选择数组np.arange(a)中的数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "cell_id": 68
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 4, 3])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.choice(a = [1,2,3,4,5], \n",
    "                 size = 3,\n",
    "                )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 69
   },
   "source": [
    "#### 10. Random.seed() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "cell_id": 71
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5488135])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(0)\n",
    "np.random.rand(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "cell_id": 72
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.71518937])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 73
   },
   "source": [
    "#### 11. Logspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "cell_id": 75
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1.,   2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0,9,10,base=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 76
   },
   "source": [
    "#### 12. Zeroes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "cell_id": 78
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((2,3),dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "cell_id": 79
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 80
   },
   "source": [
    "#### 13. Ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "cell_id": 82
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 83
   },
   "source": [
    "#### 14. Array of k Random Value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "cell_id": 85
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10, 10],\n",
       "       [10, 10, 10, 10]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,4),fill_value=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "cell_id": 86
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1],\n",
       "        [1],\n",
       "        [1],\n",
       "        [1]],\n",
       "\n",
       "       [[1],\n",
       "        [1],\n",
       "        [1],\n",
       "        [1]]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,4,1),fill_value=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 87
   },
   "source": [
    "#### 15. Identity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "cell_id": 89
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0],\n",
       "       [0, 1, 0, 0],\n",
       "       [0, 0, 1, 0],\n",
       "       [0, 0, 0, 1]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.identity(4,dtype = int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "cell_id": 90
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.identity(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 91
   },
   "source": [
    "### 操纵数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 94
   },
   "source": [
    "#### 16. min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "cell_id": 96
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1,1,2,3,3,4,5,6,6,2])\n",
    "np.min(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 97
   },
   "source": [
    "#### 17. max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "cell_id": 99
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 100
   },
   "source": [
    "#### 18. Unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "cell_id": 103
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4, 5, 6]), array([2, 2, 2, 1, 1, 2]))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(arr,return_counts=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "cell_id": 104
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4, 5, 6]),\n",
       " array([0, 2, 3, 5, 6, 7]),\n",
       " array([0, 0, 1, 2, 2, 3, 4, 5, 5, 1]),\n",
       " array([2, 2, 2, 1, 1, 2]))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(arr,return_counts=True,return_inverse = True,return_index = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 105
   },
   "source": [
    "#### 19. Mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "cell_id": 107
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(arr,dtype='int')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 108
   },
   "source": [
    "#### 20. Median"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "cell_id": 110
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [5 8 4]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[1,2,3],[5,8,4]])\n",
    "\n",
    "print(arr)\n",
    "np.median(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 112
   },
   "source": [
    "#### 21. Digitize\n",
    "\n",
    "Return the indices of the bins to which each value in the input array belongs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "cell_id": 115
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1, 2, 2, 2, 4, 4, 4])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([-0.9, 0.5, 0.9, 1, 1.2, 1.4, 3.6, 4.7, 5.3])\n",
    "bins = np.array([0,1,2,3])\n",
    "np.digitize(a,bins)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 116
   },
   "source": [
    "```\n",
    "Exp        Value\n",
    "x < 0     :   0\n",
    "0 <= x <1 :   1\n",
    "1 <= x <2 :   2\n",
    "2 <= x <3 :   3\n",
    "3 <=x     :   4\n",
    "Compares -0.9 to 0, here x < 0 so Put 0 in resulting array.\n",
    "Compares  0.5 to 0, here 0 <= x <1 so Put 1.\n",
    ".\n",
    "Compares 5.4 to 4, here 3<=x so Put 4\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 118
   },
   "source": [
    "#### 22. Reshape\n",
    "It Returns an array containing the same data with a new shape."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "cell_id": 120
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3, 11,  3],\n",
       "       [ 7,  9,  3],\n",
       "       [ 5,  2,  4],\n",
       "       [ 7,  6,  8]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.random.randint(15,size=(4,3))\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "cell_id": 121
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3, 11,  3,  7],\n",
       "       [ 9,  3,  5,  2],\n",
       "       [ 4,  7,  6,  8]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "cell_id": 122
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3, 11,  3,  7,  9,  3,  5,  2,  4,  7,  6,  8])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.reshape(-1)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 123
   },
   "source": [
    "#### 23. Expand Dimensions\n",
    "\n",
    "It is used to expand the dimensions of an array. This method is useful for creating sample test data for testing a machine learning model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "cell_id": 125
   },
   "outputs": [],
   "source": [
    "arr = np.array([ 8, 14,  1,  8, 11,  4,  9,  4, 1, 13, 13, 11])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "cell_id": 126
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8, 14,  1,  8, 11,  4,  9,  4,  1, 13, 13, 11]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.expand_dims(arr,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "cell_id": 127
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8],\n",
       "       [14],\n",
       "       [ 1],\n",
       "       [ 8],\n",
       "       [11],\n",
       "       [ 4],\n",
       "       [ 9],\n",
       "       [ 4],\n",
       "       [ 1],\n",
       "       [13],\n",
       "       [13],\n",
       "       [11]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.expand_dims(arr,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 128
   },
   "source": [
    "#### 24. Squeeze\n",
    "\n",
    "Reduce the dimension of an array by removing single-dimensional entries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "cell_id": 130
   },
   "outputs": [],
   "source": [
    "arr = np.array([[ 8],[14],[ 1],[ 8],[11],[ 4],[ 9],[ 4],[ 1],[13],[13],[11]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "cell_id": 131
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8, 14,  1,  8, 11,  4,  9,  4,  1, 13, 13, 11])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.squeeze(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 132
   },
   "source": [
    "#### 25. Count Non-Zero"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "cell_id": 134
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0,0,1,1,1,0])\n",
    "np.count_nonzero(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 135
   },
   "source": [
    "#### 26. argwhere\n",
    "\n",
    "Find and return all the indices of non-zero elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "cell_id": 137
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [6],\n",
       "       [8]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0,0,1,1,1,0,1,0,1])\n",
    "np.argwhere(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 138
   },
   "source": [
    "#### 29. argmax & argmin\n",
    "\n",
    "argmax returns the index of the max element from the array. It can be used to get the index of high probability predicted labels in multiclass image classification problems.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "cell_id": 140
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[0.12,0.64,0.19,0.05,0.64]])\n",
    "np.argmax(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "cell_id": 141
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 142
   },
   "source": [
    "#### 30. Sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "cell_id": 144
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 7])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([2,3,1,7,4,5])\n",
    "np.sort(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 145
   },
   "source": [
    "#### 31. abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "cell_id": 147
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 4],\n",
       "       [2, 4, 3]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1,-3,4],[-2,-4,3]])\n",
    "np.abs(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 148
   },
   "source": [
    "#### 32. round"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "cell_id": 150
   },
   "outputs": [],
   "source": [
    "a = np.random.random(size=(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "cell_id": 151
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.96366276, 0.38344152, 0.79172504, 0.52889492],\n",
       "       [0.56804456, 0.92559664, 0.07103606, 0.0871293 ],\n",
       "       [0.0202184 , 0.83261985, 0.77815675, 0.87001215]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "cell_id": 152
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 1., 1.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [0., 1., 1., 1.]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(a,decimals=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "cell_id": 153
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 0.4, 0.8, 0.5],\n",
       "       [0.6, 0.9, 0.1, 0.1],\n",
       "       [0. , 0.8, 0.8, 0.9]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(a,decimals=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 154
   },
   "source": [
    "#### 33. clip\n",
    "\n",
    "It is used to keep the values of an array within a range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "cell_id": 156
   },
   "outputs": [],
   "source": [
    "arr = np.array([0,1,-3,-4,5,6,7,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "cell_id": 157
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 5, 5, 5, 2, 3])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.clip(0,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "cell_id": 158
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 3, 3, 3, 2, 3])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.clip(0,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "cell_id": 159
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 3, 3, 5, 5, 5, 3, 3])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.clip(3,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 160
   },
   "source": [
    "### 数值替换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 162
   },
   "source": [
    "#### 34. where"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "cell_id": 164
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  4,  5],\n",
       "       [ 6,  7,  8],\n",
       "       [ 9, 10, 11]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(4,3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "cell_id": 165
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2, 2, 2, 3, 3, 3]), array([0, 1, 2, 0, 1, 2]))"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(a>5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "cell_id": 166
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.where(a>5)]  # get Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "cell_id": 169
   },
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(4,3).astype('float')\n",
    "a[a % 2 == 0] = 'nan'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "cell_id": 170
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[nan,  1., nan],\n",
       "       [ 3., nan,  5.],\n",
       "       [nan,  7., nan],\n",
       "       [ 9., nan, 11.]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "cell_id": 167
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 1],\n",
       "       [0, 1, 0],\n",
       "       [1, 0, 1],\n",
       "       [0, 1, 0]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.isnan(a), 1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 168
   },
   "source": [
    "#### 35. put\n",
    "Replaces specified elements of an array with given values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "cell_id": 172
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4,5,6])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "cell_id": 173
   },
   "outputs": [],
   "source": [
    "np.put(arr,[1,2],[6,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "cell_id": 174
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 6, 7, 4, 5, 6])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 175
   },
   "source": [
    "#### 36. copyto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "cell_id": 177
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before arr1 [1 2 3]\n",
      "Before arr2 [4 5 6]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.array([4,5,6])\n",
    "print(\"Before arr1\",arr1)\n",
    "print(\"Before arr2\",arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "cell_id": 178
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After arr1 [4 5 6]\n",
      "After arr2 [4 5 6]\n"
     ]
    }
   ],
   "source": [
    "np.copyto(arr1,arr2)  # from arr2 to arr1\n",
    "print(\"After arr1\",arr1)\n",
    "print(\"After arr2\",arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 180
   },
   "source": [
    "### 集合操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 181
   },
   "source": [
    "#### 37. intersect1d \n",
    "np.intersect1d function returns all the unique values from both arrays in a sorted manner.\n",
    "\n",
    " <img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Venn0001.svg/1200px-Venn0001.svg.png\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "cell_id": 186
   },
   "outputs": [],
   "source": [
    "ar1 = np.array([1,2,3,4,5,6])\n",
    "ar2 = np.array([3,4,5,8,9,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "cell_id": 182
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 4, 5])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(ar1,ar2) # 返回公共的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "cell_id": 183
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 3, 4, 5]), array([0, 2, 3, 4]), array([5, 0, 1, 2]))"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(ar1,ar2,return_indices=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 187
   },
   "source": [
    "```\n",
    "array([0, 2, 3, 4], dtype=int64) : Indices of common elements inside first array.\n",
    "array([5, 0, 1, 2], dtype=int64)) : Indices of common elements inside second array.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 190
   },
   "source": [
    "#### 38. difference\n",
    "np.setdiff1d function returns all the unique elements from array 1 that are not present in arr2.\n",
    "\n",
    " <img src=\"https://www.askpython.com/wp-content/uploads/2020/01/Set-Difference.png\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "cell_id": 192
   },
   "outputs": [],
   "source": [
    "a = np.array([1, 7, 3, 2, 4, 1])\n",
    "b = np.array([9, 2, 5, 6, 7, 8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "cell_id": 193
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 4, 1])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(a, b, assume_unique = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "cell_id": 194
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 4])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 195
   },
   "source": [
    "#### 39. union\n",
    "\n",
    "union1d function will combine both arrays into one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "cell_id": 197
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5, 36])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([1, 3, 5, 4, 36])\n",
    "np.union1d(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 199
   },
   "source": [
    "<img src=\"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Union-in-python.jpg\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 201
   },
   "source": [
    "### 分割"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 203
   },
   "source": [
    "#### 40. Horizontal Split\n",
    "hsplit function will split the data horizontally into n equal parts.\n",
    "\n",
    "<img src=\"https://www.w3resource.com/w3r_images/numpy-manipulation-array-hsplit-function-image-2.png\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "cell_id": 205
   },
   "outputs": [],
   "source": [
    "A = np.array([[3,4,5,2],[6,7,2,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "cell_id": 206
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 5, 2],\n",
       "       [6, 7, 2, 6]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "cell_id": 207
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[3, 4],\n",
       "        [6, 7]]),\n",
       " array([[5, 2],\n",
       "        [2, 6]])]"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(A,2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "cell_id": 208,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[3],\n",
       "        [6]]),\n",
       " array([[4],\n",
       "        [7]]),\n",
       " array([[5],\n",
       "        [2]]),\n",
       " array([[2],\n",
       "        [6]])]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(A,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 212
   },
   "source": [
    "#### 41. Vertical Split\n",
    "vsplit will split the data vertically into n equal parts.\n",
    "\n",
    "<img src=\"https://www.w3resource.com/w3r_images/numpy-manipulation-array-vsplit-function-image-2.png\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "cell_id": 213,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[3, 4, 5, 2]]), array([[6, 7, 2, 6]])]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[3,4,5,2],[6,7,2,6]])\n",
    "np.vsplit(A,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 216
   },
   "source": [
    "### 合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 221
   },
   "source": [
    "#### 42. Horizontal Stacking\n",
    "hstack will stack appends one array at the end of another.\n",
    "\n",
    "<img src=\"https://www.w3resource.com/w3r_images/numpy-manipulation-hstack-function-image-1.png\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "cell_id": 223
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  1,  4,  9, 16, 25])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "b = np.array([1,4,9,16,25])\n",
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 227
   },
   "source": [
    "#### 43. Vertical Stacking\n",
    "vstack will stack one array on top of another.\n",
    "\n",
    "<img src=\"https://www.w3resource.com/w3r_images/numpy-manipulation-vstack-function-image-1.png\" width = \"300\" height = \"200\" alt=\"图片名称\" align=center />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "cell_id": 230
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 1,  4,  9, 16, 25]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 231
   },
   "source": [
    "### 比较"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 232
   },
   "source": [
    "#### 44. allclose\n",
    "\n",
    "np.allclose function finds whether two arrays are equal or approximately equal to each other based on some tolerance value if the shape of both arrays is the same.\n",
    "\n",
    "If the following equation is element-wise True, then allclose returns\n",
    "True.\n",
    "\n",
    " absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "cell_id": 233
   },
   "outputs": [],
   "source": [
    "a = np.array([0.25,0.4,0.6,0.32])\n",
    "b = np.array([0.26,0.3,0.7,0.32])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "cell_id": 234
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tolerance = 0.1           ## Total Difference \n",
    "np.allclose(a,b,tolerance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "cell_id": 235
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tolerance = 0.5\n",
    "np.allclose(a,b,tolerance)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 238
   },
   "source": [
    "#### 45. equals\n",
    "\n",
    "It compares each element of both arrays and if elements matched returns True."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "cell_id": 242
   },
   "outputs": [],
   "source": [
    "arr1 = np.array([1,2,3,4,5,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "cell_id": 243
   },
   "outputs": [],
   "source": [
    "arr2 = np.array([1,2,3,5,6,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "cell_id": 240
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True, False, False, False])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.equal(arr1,arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 244
   },
   "source": [
    "### 重复"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 247
   },
   "source": [
    "#### 46. repeat\n",
    "\n",
    "It is used to repeat elements of an array for n number of times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "cell_id": 249
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['2017', '2017', '2017'], dtype='<U4')"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat('2017',3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "cell_id": 250
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Product</th>\n",
       "      <th>ContainerSales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Mango</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Apple</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Banana</td>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Product  ContainerSales\n",
       "0   Mango              40\n",
       "1   Apple              90\n",
       "2  Banana             130"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "fruits = pd.DataFrame([\n",
    "    ['Mango',40],\n",
    "    ['Apple',90],\n",
    "    ['Banana',130]\n",
    "],columns=['Product','ContainerSales'])\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "cell_id": 251
   },
   "outputs": [],
   "source": [
    "fruits['year_numpy'] = np.repeat(2020,fruits.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "cell_id": 252
   },
   "outputs": [],
   "source": [
    "fruits['year_pandas'] = 2020 # broadcast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "cell_id": 253
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Product</th>\n",
       "      <th>ContainerSales</th>\n",
       "      <th>year_numpy</th>\n",
       "      <th>year_pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Mango</td>\n",
       "      <td>40</td>\n",
       "      <td>2020</td>\n",
       "      <td>2020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Apple</td>\n",
       "      <td>90</td>\n",
       "      <td>2020</td>\n",
       "      <td>2020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Banana</td>\n",
       "      <td>130</td>\n",
       "      <td>2020</td>\n",
       "      <td>2020</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Product  ContainerSales  year_numpy  year_pandas\n",
       "0   Mango              40        2020         2020\n",
       "1   Apple              90        2020         2020\n",
       "2  Banana             130        2020         2020"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 254
   },
   "source": [
    "#### 47. tile\n",
    "\n",
    "Construct an array by repeating A the number of times given by reps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "cell_id": 257
   },
   "outputs": [],
   "source": [
    "arr = np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "cell_id": 256
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tile</th>\n",
       "      <th>repeat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   tile  repeat\n",
       "0     1       1\n",
       "1     2       1\n",
       "2     3       1\n",
       "3     1       2\n",
       "4     2       2\n",
       "5     3       2\n",
       "6     1       3\n",
       "7     2       3\n",
       "8     3       3"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(\n",
    "    {\n",
    "        'tile': np.tile(arr,3),\n",
    "        'repeat':np.repeat(arr,3)\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 261
   },
   "source": [
    "### 代数计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 263
   },
   "source": [
    "#### 48. Einsum\n",
    "This function is used to compute many multi-dimensional and linear algebraic operations on arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "cell_id": 265
   },
   "outputs": [],
   "source": [
    "a = np.arange(1,10).reshape(3,3)\n",
    "b = np.arange(21,30).reshape(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "cell_id": 266
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "cell_id": 267
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[21, 22, 23],\n",
       "       [24, 25, 26],\n",
       "       [27, 28, 29]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "cell_id": 268
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 5, 9])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ii->i',a) # ᴇxᴛʀᴀᴄᴛꜱ ᴅɪᴀɢᴏɴᴀʟꜱ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "cell_id": 269
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 7],\n",
       "       [2, 5, 8],\n",
       "       [3, 6, 9]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ji',a) # ᴛʀᴀɴꜱᴘᴏꜱᴇ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "cell_id": 270
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[150, 156, 162],\n",
       "       [366, 381, 396],\n",
       "       [582, 606, 630]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ij,jk',a,b) # ᴍᴀᴛʀɪx ᴍᴜʟᴛɪᴘɪᴄᴀᴛɪᴏɴ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "cell_id": 271
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ii',a) # ꜱᴜᴍ ᴏꜰ ᴅɪᴀɢᴏɴᴀʟ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 272
   },
   "source": [
    "### 统计分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 274
   },
   "source": [
    "#### 49. Histogram\n",
    "It is an important statistical analysis function of NumPy that computes histogram values for a set of data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "cell_id": 277
   },
   "outputs": [],
   "source": [
    "A = np.array([[3, 4, 5, 2],\n",
    "              [6, 7, 2, 6]])\n",
    "hist,bins = np.histogram(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "cell_id": 278
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 1, 0, 1, 0, 1, 0, 2, 1])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "cell_id": 279
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. ])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "cell_id": 280
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Duplicate key in file PosixPath('/opt/conda/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'), line 768 ('font.family  : sans-serif')\n",
      "Duplicate key in file PosixPath('/opt/conda/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'), line 769 ('font.sans-serif : SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif')\n",
      "Duplicate key in file PosixPath('/opt/conda/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'), line 770 ('axes.unicode_minus : False')\n"
     ]
    },
    {
     "data": {
      "image/png": "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\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt \n",
    "import numpy as np  \n",
    "   \n",
    "a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) \n",
    "plt.hist(a, bins = [0,20,40,60,80,100]) \n",
    "plt.title(\"histogram\") \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 281
   },
   "source": [
    "#### 50. Percentile\n",
    "\n",
    "It computes the q-th percentile of the data along a specified axis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "cell_id": 283
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[2, 4, 6], [4, 8, 12]])\n",
    "np.percentile(a, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "cell_id": 284
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.percentile(a, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "cell_id": 285
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([2,3,4,1,6,7])\n",
    "np.percentile(a,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 286
   },
   "source": [
    "#### 51. Standard Deviation "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "cell_id": 288
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [ 4,  8, 12]])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[2, 4, 6], [4, 8, 12]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "cell_id": 289
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.63299316, 3.26598632])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(a,axis=1)    ## Row Wise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "cell_id": 290
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3.])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(a,axis=0)    ## Column Wise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 291
   },
   "source": [
    "#### 52. Variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "cell_id": 293
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.66666667, 10.66666667])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(a,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "cell_id": 294
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 4., 9.])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(a,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 295
   },
   "source": [
    "### 输出设置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 299
   },
   "source": [
    "#### 53. 小数保留"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "cell_id": 297
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12.23 32.35]\n"
     ]
    }
   ],
   "source": [
    "np.set_printoptions(precision=2)\n",
    "a = np.array([12.23456,32.34535])\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 302
   },
   "source": [
    "#### 54. 全部展示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "cell_id": 298
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17\n",
      "  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35\n",
      "  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53\n",
      "  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71\n",
      "  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89\n",
      "  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107\n",
      " 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125\n",
      " 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143\n",
      " 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161\n",
      " 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179\n",
      " 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197\n",
      " 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215\n",
      " 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233\n",
      " 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251\n",
      " 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269\n",
      " 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287\n",
      " 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305\n",
      " 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323\n",
      " 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341\n",
      " 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359\n",
      " 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377\n",
      " 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395\n",
      " 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413\n",
      " 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431\n",
      " 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449\n",
      " 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467\n",
      " 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485\n",
      " 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503\n",
      " 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521\n",
      " 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539\n",
      " 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557\n",
      " 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575\n",
      " 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593\n",
      " 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611\n",
      " 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629\n",
      " 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647\n",
      " 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665\n",
      " 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683\n",
      " 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701\n",
      " 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719\n",
      " 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737\n",
      " 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755\n",
      " 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773\n",
      " 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791\n",
      " 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809\n",
      " 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827\n",
      " 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845\n",
      " 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863\n",
      " 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881\n",
      " 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899\n",
      " 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917\n",
      " 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935\n",
      " 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953\n",
      " 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971\n",
      " 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989\n",
      " 990 991 992 993 994 995 996 997 998 999]\n"
     ]
    }
   ],
   "source": [
    "np.set_printoptions(threshold=np.inf)\n",
    "a = np.arange(1000)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 301
   },
   "source": [
    "#### 增加宽度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "cell_id": 306
   },
   "outputs": [],
   "source": [
    "np.set_printoptions(linewidth=100) ## Default 75"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 307
   },
   "source": [
    "### 存储导入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 312
   },
   "source": [
    "#### 55. savetxt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "cell_id": 309
   },
   "outputs": [],
   "source": [
    "arr = np.linspace(10,100,10).reshape(5,2) \n",
    "np.savetxt('array.txt',arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 314
   },
   "source": [
    "#### 56. loadtxt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "cell_id": 310,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 10.,  20.],\n",
       "       [ 30.,  40.],\n",
       "       [ 50.,  60.],\n",
       "       [ 70.,  80.],\n",
       "       [ 90., 100.]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('array.txt') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 317
   },
   "source": [
    "### 补充"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 319
   },
   "source": [
    "#### 57. flatten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "cell_id": 320
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2], [3,4]])\n",
    "a.flatten()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 325
   },
   "source": [
    "#### 58. ceil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "cell_id": 321
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1., -1., -0.,  1.,  2.,  2.,  2.])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])\n",
    "np.ceil(a) # 向上取整"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 327
   },
   "source": [
    "#### 59. floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "cell_id": 323
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2., -2., -1.,  0.,  1.,  1.,  2.])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(a) # 向下取整"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 330
   },
   "source": [
    "#### 60. rint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "cell_id": 343
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2., -2., -0.,  0.,  2.,  2.,  2.])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rint(a) # 四舍五入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 344
   },
   "source": [
    "#### 61. sign"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "cell_id": 324
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1., -1.,  0.,  1.,  1.,  1.,  1.])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([-1.7, -1.5, 0, 0.1, 1.5, 3.7, 2.0])\n",
    "np.sign(a)  # 计算各元素的符号值 1（+），0，-1（-）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 334
   },
   "source": [
    "#### 62. mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "cell_id": 329
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1, 24])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mod([4, 7, 100], [2, 3, 76]) # 余数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 337
   },
   "source": [
    "#### 63. floor_divide"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "cell_id": 333
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 1])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor_divide([4, 7, 100], [2, 3, 76]) # 除数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 339
   },
   "source": [
    "#### 64. shuffle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "cell_id": 336
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 6, 9, 4, 5, 8, 1, 0, 3, 2])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "np.random.shuffle(arr)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": 342
   },
   "source": [
    "#### 65. argsort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "cell_id": 347
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 0])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([3, 1, 2])\n",
    "np.argsort(x)  # 返回排序后的值在原数组中的index"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "max_cell_id": 350,
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
